#region License
/*
 * Copyright (c) 2007, Matthew Holmes
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice, this
 * list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 *
 * * Neither the name of The Mango Project nor the names of its contributors may be
 * used to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using Mogre;

namespace Mango.Framework.Common {
    public class OgreManager : IDisposable {
        #region Fields

        private Root m_Root = null;
        private SceneManager m_SceneMgr = null;

        private Dictionary<string, IOgreConsumer> m_Consumers = new Dictionary<string, IOgreConsumer> ();

        #endregion

        #region Constructors

        public OgreManager () {
            if (Singleton.HasRef<OgreManager> ()) {
                throw new InvalidOperationException ("OgreManager already created");
            }

            Singleton.Register<OgreManager> (this);
        }

        #endregion

        #region Properties

        public Root Root {
            get {
                return m_Root;
            }
        }

        public SceneManager SceneManager {
            get {
                return m_SceneMgr;
            }
        }

        #endregion

        #region Public Methods

        public void Initialize() {
            m_Root = new Root ("", "", "mangogfx.log");

#if DEBUG
            m_Root.LoadPlugin ("RenderSystem_Direct3D9_d");
            m_Root.LoadPlugin ("RenderSystem_GL_d");
            m_Root.LoadPlugin ("Plugin_ParticleFX_d");
#else
            m_Root.LoadPlugin ("RenderSystem_Direct3D9");
            m_Root.LoadPlugin ("RenderSystem_GL");
            m_Root.LoadPlugin ("Plugin_ParticleFX");
#endif

            RenderSystem rs = m_Root.GetRenderSystemByName ("Direct3D9 Rendering Subsystem");
            m_Root.RenderSystem = rs;
            rs.SetConfigOption ("Full Screen", "No");
            rs.SetConfigOption ("Video Mode", "800 x 600 @ 32-bit colour");

            m_SceneMgr = m_Root.CreateSceneManager (SceneType.ST_GENERIC);

            m_Root.Initialise (false);
            ResetResources ();
        }

        public void RegisterConsumer(IOgreConsumer oc) {
            if(m_Consumers.ContainsKey(oc.InstanceName)) {
                throw new InvalidOperationException ("Consumer instance already exists");
            }

            m_Consumers[oc.InstanceName] = oc;
        }

        public void ResetResources() {
            StringVector groups = ResourceGroupManager.Singleton.GetResourceGroups ();
            foreach(string group in groups) {
                ResourceGroupManager.Singleton.DestroyResourceGroup (group);
            }

            ResourceGroupManager.Singleton.AddResourceLocation (Path.Combine (Environment.CurrentDirectory, "Data\\MangoCore.zip"), "Zip", "Kernel");
            ResourceGroupManager.Singleton.InitialiseResourceGroup ("Kernel");
        }

        #endregion

        #region IDisposable Members

        public void Dispose () {
            if(m_Root != null) {
                foreach(KeyValuePair<string, IOgreConsumer> kvp in m_Consumers) {
                    kvp.Value.Dispose ();
                }

                m_Root.Dispose ();
                m_Root = null;
            }
        }

        #endregion
    }
}
